<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="地区名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['regionName']" placeholder="请输入地区名称" :disabled="true"></a-input>
        </a-form-item>
        <a-form-item label="地区等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-model="model.regionGrade"
                    placeholder="请选择地区等级" :allowClear="true" @change="getListByRegionGrade" :disabled="true">
            <a-select-option :value="1">省</a-select-option>
            <a-select-option :value="2">市</a-select-option>
            <a-select-option :value="3">区/县</a-select-option>
            <a-select-option :value="4">街道</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="上级地区" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="model.regionGrade > 1">
          <a-select v-model="model.regionPid"
                    placeholder="请选择上级地区" :allowClear="true" :disabled="true">
            <a-select-option v-for="d in regionInfoList"
                             :key="d.id"
                             :value="d.id">{{ d.regionName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="热门城市" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch checked-children="是" un-checked-children="否" default-checked @change="switchOn"  v-model="checked"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {httpAction, getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: "SmsRegionInfoModal",
    components: {
      JDate,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        checked:true,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: "/hospital/smsRegionInfo/add",
          edit: "/hospital/smsRegionInfo/edit",
          getListByPidAndGrade: "/hospital/smsRegionInfo/getListByPidAndGrade",
        },
        regionInfoList: [],
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        if (record.regionGrade > 1){
          let grand = record.regionGrade -1;
          this.getListByPidAndGrade(grand);
        }
        if (record){
          this.checked = record.popularCities  == 0 ? false : true;
        }else {
          //设置默认展示
          this.model.showStatus = 1;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'regionName', 'regionGrade', 'regionPid', 'regionCode', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag', 'popularCities'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'regionName', 'regionGrade', 'regionPid', 'regionCode', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag', 'popularCities'))
      },
      getListByRegionGrade() {
        if (this.model.regionGrade > 1) {
          let grade = this.model.regionGrade - 1;
          this.getListByPidAndGrade(grade);
        }
      },
      getListByPidAndGrade(grade){
        getAction(this.url.getListByPidAndGrade, {grade: grade}).then((res) => {
          if (res.success) {
            this.regionInfoList = res.result;
          }
        });
      },
      switchOn(checked){
        if (checked){
          this.model.popularCities = 1;
          this.checked = true;
        } else {
          this.model.popularCities = 0;
          this.checked = false;
        }
      },
    }
  }
</script>